package com.example.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.ResultCode;
import com.example.dao.*;
import com.example.entity.Guoyuanxinxi;
import com.example.exception.CustomException;
import com.example.service.GuoyuanxinxiService;
import com.example.vo.GuoyuanxinxiVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Map;

@Service("guoyuanxinxiService")
public class GuoyuanxinxiServiceImpl extends ServiceImpl<GuoyuanxinxiDao, Guoyuanxinxi> implements GuoyuanxinxiService {

    @Resource
    private GuoyuanxinxiDao guoyuanxinxiDao;

    public Guoyuanxinxi add(Guoyuanxinxi guoyuanxinxi) {
		
	   //youquchong1
        guoyuanxinxi.setAddtime(new Date());
        guoyuanxinxiDao.insert(guoyuanxinxi);
        return guoyuanxinxi;
    }
	
	
	
    public void delete(Long id) {
        guoyuanxinxiDao.deleteById(id);
    }
	

    public void update(Guoyuanxinxi guoyuanxinxi) {
        //youquchong2
        guoyuanxinxiDao.updateById(guoyuanxinxi);
    }
	
	public void updateByzhujian(Guoyuanxinxi guoyuanxinxi) {
        LambdaQueryWrapper<Guoyuanxinxi> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtils.isNotEmpty(guoyuanxinxi.getBianhao()), Guoyuanxinxi::getBianhao,guoyuanxinxi.getBianhao());

        guoyuanxinxiDao.update(guoyuanxinxi, wrapper);
    }
	
	@Override
    public List<Map<String, Object>> daochuexcel() {
        return guoyuanxinxiDao.daochuexcel();
    }

    @Override
    public Guoyuanxinxi findById(Integer id) {
        return guoyuanxinxiDao.selectById(id);
    }
	
	@Override
    public Guoyuanxinxi getByzhujian(String zhujian) {
        return guoyuanxinxiDao.getByzhujian(zhujian);
    }

    public GuoyuanxinxiVo findPage(GuoyuanxinxiVo guoyuanxinxiVo) {
        LambdaQueryWrapper<Guoyuanxinxi> wrapper = Wrappers.lambdaQuery();
		wrapper.like(ObjectUtils.isNotEmpty(guoyuanxinxiVo.getBianhao()), Guoyuanxinxi::getBianhao,guoyuanxinxiVo.getBianhao());        wrapper.like(ObjectUtils.isNotEmpty(guoyuanxinxiVo.getMingcheng()), Guoyuanxinxi::getMingcheng,guoyuanxinxiVo.getMingcheng());        wrapper.like(ObjectUtils.isNotEmpty(guoyuanxinxiVo.getMianji()), Guoyuanxinxi::getMianji,guoyuanxinxiVo.getMianji());        wrapper.like(ObjectUtils.isNotEmpty(guoyuanxinxiVo.getZhongzhiwu()), Guoyuanxinxi::getZhongzhiwu,guoyuanxinxiVo.getZhongzhiwu());        wrapper.like(ObjectUtils.isNotEmpty(guoyuanxinxiVo.getCaizhaizhouqi()), Guoyuanxinxi::getCaizhaizhouqi,guoyuanxinxiVo.getCaizhaizhouqi());        wrapper.like(ObjectUtils.isNotEmpty(guoyuanxinxiVo.getDanjia()), Guoyuanxinxi::getDanjia,guoyuanxinxiVo.getDanjia());        wrapper.like(ObjectUtils.isNotEmpty(guoyuanxinxiVo.getTupian()), Guoyuanxinxi::getTupian,guoyuanxinxiVo.getTupian());        wrapper.like(ObjectUtils.isNotEmpty(guoyuanxinxiVo.getDizhi()), Guoyuanxinxi::getDizhi,guoyuanxinxiVo.getDizhi());        
		wrapper.orderByDesc(Guoyuanxinxi::getId);
        Page<Guoyuanxinxi> page = new Page<>(guoyuanxinxiVo.getCurrent(),guoyuanxinxiVo.getCurrentNum());
        guoyuanxinxiDao.selectPage(page,wrapper);
        List<Guoyuanxinxi> guoyuanxinxis = page.getRecords();
        guoyuanxinxiVo.setList(guoyuanxinxis);
        guoyuanxinxiVo.setPages(page.getPages());
        guoyuanxinxiVo.setTotal(page.getTotal());
        guoyuanxinxiVo.setCurrent(page.getCurrent());
        guoyuanxinxiVo.setCurrentNum(page.getSize());
        return guoyuanxinxiVo;
    }

    @Override
    public void deleteList(List<Guoyuanxinxi> list) {
        List<Integer> ids =  list.stream().map(Guoyuanxinxi::getId).collect(Collectors.toList());
        guoyuanxinxiDao.deleteBatchIds(ids);
    }
	
	
	
	
	
	//youxxianxci
}
